package com.itany.book.service.impl;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.itany.constant.Constants;
import com.itany.mapper.BookMapper;
import com.itany.mapper.ExamineMapper;
import com.itany.pojo.Book;
import com.itany.pojo.Examine;
import com.itany.pojo.ManagerUser;
import com.itany.service.BookService;
import com.itany.utils.StringUtils;
import com.itany.vo.BookVo;
import org.springframework.stereotype.Service;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.multipart.MultipartFile;

import javax.annotation.Resource;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.util.List;


/**
 * Author:梁辰
 * 时间:19-4-2 上午9:52
 * description:
 * version:1.0
 */
@Service
public class BookServiceImpl implements BookService {
    @Resource
  private   BookMapper bookMapper;
    @Resource
   private ExamineMapper examineMapper;


    public void addBook(Book book, ManagerUser user,@RequestParam(required = false) MultipartFile file) throws FileNotFoundException {
        Book isbn = bookMapper.selectByISBN(book.getISBN());
        Examine examine = new Examine();
        examine.setNumber(book.getNumber());
        if(null==isbn){
            book.setNumber(0);
            try {
                String originalFilename = file.getOriginalFilename();
                file.transferTo(new File("/home/soft01/apache-tomcat-7.0.40/webapps/image/"+originalFilename));
//                file.transferTo(new File("D:/image/"+originalFilename));
                book.setImgurl(originalFilename);
                book.setFlag(Constants.DISABLE_BOOK_FLAG);
                bookMapper.insertBook(book);
                examine.setBookid(book.getId());
            } catch (IOException e) {
                throw new FileNotFoundException("资源不能为空");
            }
        }else {
            examine.setBookid(isbn.getId());
        }
        examine.setInfo("");
        examine.setSupplierid(user.getSupplierid());
        examine.setTitle("["+book.getBookname()+"]审核");
        examine.setFlag(Constants.DEFAULT_EXAMINE_FLAG);
        examineMapper.insertExamine(examine);
    }
    @Override
    public PageInfo<BookVo> findBookByFuzzy(BookVo vo, Integer page, Integer rows) {
        vo.setBookname(StringUtils.formatLike(vo.getBookname()));
        vo.setISBN(StringUtils.formatLike(vo.getISBN()));
        PageHelper.startPage(page,rows);
        List<BookVo>bookVos=bookMapper.selectBookByFuzzy(vo);
        PageInfo<BookVo>pageInfo=new PageInfo<BookVo>(bookVos);
        return pageInfo;
    }

    @Override
    public void modifyEnFlagById(Book book) {
        bookMapper.updateEnFlagById(book);

    }

    @Override
    public void modifyDisFlagById(Book book) {
        bookMapper.updateDisFlagById(book);

    }

    @Override
    public void modifyById(Book book, MultipartFile file) throws FileNotFoundException {
        if (null==file){
            try {
                String originalFilename = file.getOriginalFilename();

                file.transferTo(new File("/home/soft01/apache-tomcat-7.0.40/webapps/image"+originalFilename));
                book.setImgurl(originalFilename);
                bookMapper.updateById(book);
            } catch (IOException e) {
            throw new FileNotFoundException("资源不能为空");

        }

        }
        bookMapper.updateById(book);
    }
}
